package com.fulihui.wgj.admin.api.wxtemplate;

import com.fulihui.common.builder.web.JsonResultBuilder;
import com.fulihui.common.result.web.PageView;
import com.fulihui.common.result.web.Result;
import com.fulihui.wgj.admin.api.base.BaseController;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateDeleteRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplatePageQueryRequest;
import com.fulihui.wgj.admin.api.wxtemplate.request.TemplateSelectRequest;
import com.fulihui.wgj.admin.api.wxtemplate.vo.WxTemplateDataVO;
import com.fulihui.wgj.admin.api.wxtemplate.vo.WxTemplateVO;
import com.fulihui.wgj.admin.integration.WxTemplateServiceClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Objects;

/**
 * @author: Levon
 * @date: 2018-07-25 16:34
 */
@RestController
@RequestMapping("/template")
@Api(tags = "微信模板消息相关接口")
public class TemplateMsgController extends BaseController {

    @Autowired
    private WxTemplateServiceClient wxTemplateServiceClient;

    @RequestMapping(value = "/sync", method = RequestMethod.POST)
    @ApiOperation("同步模板")
    public Result<Boolean> sync(HttpServletRequest servletRequest) {
        Result<Boolean> result = wxTemplateServiceClient.syncTemplates(getAppid(servletRequest));
        return result;
    }

    @RequestMapping(value = "/deputyList", method = RequestMethod.POST)
    @ApiOperation("获取行业列表")
    public Result<List<String>> queryDeputy(HttpServletRequest servletRequest) {
        Result<List<String>> result = wxTemplateServiceClient.queryDeputyIndustry(getAppid(servletRequest));
        return result;
    }

    @RequestMapping(value = "/templateList", method = RequestMethod.POST)
    @ApiOperation("获取模板列表")
    public Result<PageView<WxTemplateVO>> templates(@RequestBody @Valid TemplatePageQueryRequest request, BindingResult validResult,
                                                    HttpServletRequest servletRequest) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        request.setAppId(getAppid(servletRequest));
        PageView<WxTemplateVO> pageView = wxTemplateServiceClient.queryWxTemplates(request);
        return JsonResultBuilder.succ(pageView);
    }

    @RequestMapping(value = "/del", method = RequestMethod.POST)
    @ApiOperation("删除模板")
    public Result<Boolean> del(@RequestBody @Valid TemplateDeleteRequest request, BindingResult validResult,
                               HttpServletRequest servletRequest) {

        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        return wxTemplateServiceClient.delTemplate(request);
    }

    @RequestMapping(value = "/lately", method = RequestMethod.POST)
    @ApiOperation("获取最近选择的模板")
    public Result<WxTemplateDataVO> lately(HttpServletRequest servletRequest) {
        String loginId = getLoginId();
        Result<WxTemplateDataVO> result = wxTemplateServiceClient.queryLately(loginId);
        return result;
    }

    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @ApiOperation("选择一个模板")
    public Result<WxTemplateDataVO> select(@RequestBody @Valid TemplateSelectRequest request,
                                           BindingResult validResult) {
        Result result = valid(validResult);
        if (Objects.nonNull(result)) {
            return result;
        }
        String templateId = request.getTemplateId();
        String loginId = getLoginId();
        return wxTemplateServiceClient.selectTemplate(loginId, templateId);
    }

}
